<?php
class Admin_contact extends CI_Controller {

    /**
    * name of the folder responsible for the views 
    * which are manipulated by this controller
    * @constant string
    */
    const VIEW_FOLDER = 'contact';
 
    /**
    * Responsable for auto load the model
    * @return void
    */
    public function __construct()
    {
        parent::__construct();
        $this->load->model('contact_model');
    }
 
    /**
    * Load the main view with all the current model model's data.
    * @return void
    */
    public function index()
    {
		$data['contact'] = $this->contact_model->get_contact();
		
		$data['main_content'] = 'contact/add';
        $this->load->view('includes/template', $data);
    }//index

    public function add()
    {
        //if save button was clicked, get the data sent via post
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {

            //form validation
            $this->form_validation->set_rules('name', 'name', 'required');
			$this->form_validation->set_rules('telepon', 'telepon', 'trim|required|numeric');
			$this->form_validation->set_rules('email', 'email', 'trim|required|valid_email');
			$this->form_validation->set_rules('masukan', 'masukan', 'trim|required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
            

            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
                $data_to_store = array(
                    'name' => $this->input->post('name'),
                    'telepon' => $this->input->post('telepon'),
                    'email' => $this->input->post('email'),
                    'masukan' => $this->input->post('masukan')
                );
                //if the insert has returned true then we show the flash message
                if($this->contact_model->store_contact($data_to_store)){
                    $data['flash_message'] = TRUE; 
                }else{
                    $data['flash_message'] = FALSE; 
                }

            }

        }
        //load the view
        $data['main_content'] = 'contact/add';
        $this->load->view('includes/template', $data);  
    }       

}